<template>
	<cl-crud ref="crud" @load="onLoad" :on-refresh="onRefresh">
		<el-row class="h-100" type="flex">
			<cl-biz-toolbar v-model="currBiz.biz_id" @change="bizChange" />
			<cl-flex1 class="h-100" style="overflow: hidden">
				<cl-pagefull @resize="onResize">
					<template #head>
						<el-row type="flex">
							<div class="title">{{ currBiz.biz_name }}</div>
							<cl-flex1 />
							<div>
								<cl-filter-group v-model="search">
									<cl-filter label="关键字">
										<el-input
											v-model="search.tname"
											placeholder="请输入标签名称"
											clearable
											@change="refresh()"
										/>
									</cl-filter>
									<cl-filter label="状态">
										<cl-select
											v-model="search.status"
											placeholder="请选择"
											style="width: 100px"
											clearable
											@change="refresh()"
										>
											<el-option label="全部" value="" />
											<el-option label="正常" :value="1" />
											<el-option label="禁用" :value="0" />
										</cl-select>
									</cl-filter>
									<cl-filter label="类型">
										<cl-select
											v-model="search.type"
											placeholder="请选择"
											style="width: 100px"
											clearable
											@change="refresh()"
										>
											<el-option label="全部" value="" />
											<el-option label="服务" :value="1" />
											<el-option label="产品" :value="2" />
											<el-option label="卡项" :value="3" />
											<el-option label="其他" :value="4" />
										</cl-select>
									</cl-filter>
								</cl-filter-group>
								<el-button icon="el-icon-plus" size="small" type="primary" @click="add">
									新增标签
								</el-button>
							</div>
						</el-row>
					</template>
					<cl-table class="list" v-bind="table">
						<cl-empty slot="empty" />
						<template #column-status="{ scope: { row } }">
							<el-tag v-if="row.status == 1">正常</el-tag>
							<el-tag type="danger" v-else>禁用</el-tag>
						</template>
						<template #column-opt="{ scope: { row } }">
							<div class="flex flex-align-start">
								<el-button type="primary" @click="edit(row)">修改</el-button>
								<el-button type="danger" @click="del(row)"> 删除 </el-button>
							</div>
						</template>
					</cl-table>
					<template #foot>
						<el-row type="flex">
							<cl-flex1 />
							<zm-pagination />
						</el-row>
					</template>
				</cl-pagefull>
			</cl-flex1>
		</el-row>
		<!-- 添加标签 -->
		<tag-form ref="form" @confirm="refresh" />
	</cl-crud>
</template>
<script>
import TagForm from "./tag-form";
export default {
	components: {
		TagForm
	},
	data() {
		return {
			table: {
				"context-menu": ["refresh"],
				props: {
					maxHeight: 500
				},
				columns: [
					{
						type: "index",
						label: "#",
						align: "center",
						width: 50,
						fixed: true
					},
					{
						label: "事业部",
						prop: "biz_name",
						minWidth: 120,
						align: "left",
						showOverflowTooltip: true
					},
					{
						label: "标签名称",
						prop: "tname",
						minWidth: 120,
						align: "left",
						showOverflowTooltip: true
					},
					{ label: "标签类型", prop: "type_name", width: 100 },
					{ label: "标签状态", prop: "status", width: 100 },
					{ label: "操作", prop: "opt", width: 150, fixed: "right" }
				]
			},
			search: {
				status: "",
				tname: ""
			},
			type: "add",
			saving: false,
			currBiz: {
				biz_id: ""
			},
			load: false
		};
	},

	methods: {
		onResize(h) {
			this.table.props.maxHeight = h;
		},
		onLoad({ ctx, app }) {
			ctx.service(this.$service.tag).done();
			if (!this.load) return;
			app.refresh(this.search);
		},
		async onRefresh(params, { next, render }) {
			const { biz_id } = this.currBiz;
			const { list } = await next({
				...params,
				biz_id
			});
			render(list);
		},
		// crud刷新
		refresh(biz_id, reset = true) {
			biz_id && (this.currBiz.biz_id = biz_id);
			let params = { ...this.search };
			if (reset) {
				params.page = 1;
			}
			this.$refs.crud.refresh(params);
			this.load = true;
		},
		// 切换事业部
		bizChange(v) {
			this.currBiz = v;
			this.search.keyword = "";
			this.$nextTick(() => {
				this.refresh();
			});
		},
		//删除
		del({ id }) {
			this.confirm("此操作将删除该标签, 是否继续?")
				.then(() => {
					this.$service.tag.del({ id }).then(res => {
						this.refresh();
						this.$message.success("删除成功!");
					});
				})
				.catch(() => {});
		},
		// 修改
		edit(row) {
			this.$refs.form.open("edit", row);
		},
		add() {
			this.$refs.form.open("add");
		}
	}
};
</script>
<style lang="scss" scoped>
.title {
	font-size: 18px;
}
</style>
